﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QuanLyDuAn.BUS;
using QuanLyDuAn.DataAccess;
using QuanLyDuAn.DTO;
using System.Collections;

namespace QuanLyDuAn.GUI
{
    public partial class FrmChangePass : DevComponents.DotNetBar.Office2007Form
    {
        AdminController controller = new AdminController();
        InterviewerController itvController = new InterviewerController();
        ArrayList list,itvList;
        public FrmChangePass()
        {
            InitializeComponent();

        }
        public void ChangePass() {
            String user = txtusername.Text;
            String newPassword = txtNewPassword.Text;
            String confirmPassword = txtConfirmPassword.Text;
            if (newPassword.CompareTo("") != 0 && confirmPassword.CompareTo("") != 0)
            {
                if (newPassword.CompareTo(confirmPassword) == 0)
                {
                    if (this.CheckUser() == 1)
                    {
                        controller.ChangePass(user, newPassword);
                        MessageBox.Show("Change Password is successful !");
                        this.Visible = false;
                    }
                    else
                    {
                        itvController.ChangePassInterviewer(user, newPassword);
                        MessageBox.Show("Change Password is successful !");
                        this.Visible = false;
                    }
                }
                else
                {
                    MessageBox.Show("Please enter confirm password again");
                }
            }
            else
            {
                MessageBox.Show("Password is not empty");
            }
        }
        private void btnSubmit_Click(object sender, EventArgs e)
        {
            this.ChangePass();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Visible = false;
        }
        /// <summary>
        /// Load Form from Admin
        /// </summary>
        public void LoadFromAdmin()
        {
            list = controller.LoadAdminByUser(txtusername.Text.ToString());
            foreach (Admin i in list)
            {
                String pass = i.password;
                int per = i.permission;
                txtpermission.Text = per.ToString();
                txtCurrentPassword.Text = pass;
                txtusername.Enabled = false;
                txtCurrentPassword.Enabled = false;
                txtpermission.Enabled = false;
            }
        }
        /// <summary>
        /// Load Form from Interviewer
        /// </summary>
        public void LoadFromInterviewer()
        {
            itvList = itvController.GetInterviewerByUser(txtusername.Text.ToString());
            foreach (Interviewer i in itvList)
            {
                String pass = i.itvPassword;
                int per = i.pid;
                txtpermission.Text = per.ToString();
                txtCurrentPassword.Text = pass;
                txtusername.Enabled = false;
                txtCurrentPassword.Enabled = false;
                txtpermission.Enabled = false;
            }
        }
        /// <summary>
        /// Check UserName To Load Form
        /// </summary>
        public int CheckUser()
        {
            int check = 0;
            String username = txtusername.Text;
            itvList = itvController.GetInterviewerByUser(txtusername.Text.ToString());
            list = controller.LoadAdminByUser(txtusername.Text.ToString());
            foreach (Admin j in list)
            {
                if (j.userName.CompareTo(username) == 0)
                {
                    check = 1;
                }
                else
                {
                    check = 0;
                }
            }
            return check;
        }
        private void FrmChangePass_Load(object sender, EventArgs e)
        {
            if (this.CheckUser() == 1)
            {
                this.LoadFromAdmin();
            }
            else
            {
                this.LoadFromInterviewer();
            }
        }
        
    }
}
